/***************************************************************************
* Detaillingflea.cpp
*
*  July 2007
*  Copyright  2007  RuijinZheng,China
*  Email mebetop@126.com
****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include "Detailingflea.hpp"
 Detailingflealayer *Detailingflealayer::Getonlyinstance(const old_connect *origconnect,
             const old_recv *origrecv,const old_send *origsend,const old_recvfrom *origrecvfrom,
             const old_sendto *origsendto)
{
       static Detailingflealayer detailingflea(origconnect,origrecv,origsend,origrecvfrom,origsendto);

       return  &detailingflea;
}

const Proxyserver *Detailingflealayer::Getproxyserver(const vector<Proxyserver> &availableserver)
{ 
       if(availableserver.size()<1)
            return NULL;
       pthread_mutex_lock(&mut);
       if(curproxy==availableserver.size())
            curproxy=0;
       int tag=curproxy;
       curproxy++;
       pthread_mutex_unlock(&mut);
       return &availableserver.at(tag);
}

int Detailingflealayer::Connectflea(const int &sockfd,const  sockaddr *serv_addr,const vector<Proxyserver> &servers)
{
       int ret=-1;
       bool actionresult=false;
       const Proxyserver * proxyserver = Getproxyserver(servers);
       if(proxyserver!=NULL){
    	      ret = Connecttoserver(sockfd,proxyserver->sServername,proxyserver->iPort);
       }else{
      	   Errordeliver("no suitable proxy server provied","please edit proxy server list solve this problem");
      	   return -1;
         }
       if(ret<0){
       	return ret;
        }
       if(proxyserver->sType=="Socks4")
         {
    	      actionresult=Actsocks4proxy(sockfd,*((const sockaddr_in *)(serv_addr)));
         }
       else if(proxyserver->sType=="Socks4a")
         {
    	      actionresult=Actsocks4aproxy(sockfd,*((const sockaddr_in *)(serv_addr)));
         }
       else if(proxyserver->sType=="Socks5")
         {
    	      if((actionresult=Prepareforsocks5proxy(sockfd,proxyserver->sUser,proxyserver->sPasswd))==false){
    	    	     return -1;
    	       }else{
    	    	   actionresult=Actsocks5proxy(sockfd,*((const sockaddr_in *)(serv_addr)));
    	       }
         }
       else if(proxyserver->sType=="Http1.1")
        {
    	      actionresult=Acthttpproxy(sockfd,*((const sockaddr_in *)(serv_addr)),
             proxyserver->sUser,proxyserver->sPasswd);
        }
       if(actionresult==true){
            return ret;
       }else{
    	      return -1;
         }
}

Detailingflealayer::Detailingflealayer(const old_connect *origconnect,const old_recv *origrecv,
       const old_send *origsend,const old_recvfrom *origrecvfrom,const old_sendto *origsendto)
           :Lowlayer(origconnect,origrecv,origsend,origrecvfrom,origsendto)
{
       curproxy=0;
       pthread_mutex_init(&mut,NULL);
}
